import * as types from '../actionTypes/articleTypes';
import {
  getArticleChannelsList,
  getArticleList,
  deleteArticle,
} from '@/api/article';
import { message as Message } from 'antd';

export const getArticleChannelsAction = () => {
  return async dispatch => {
    const res = await getArticleChannelsList();
    dispatch({
      type: types.GET_ARTICLE_CHANNELS_LIST,
      payload: res.data.data.channels,
    });
  };
};

export const getArticleListAction = data => {
  return async dispatch => {
    const res = await getArticleList(data);
    const {
      page,
      per_page: pageSize,
      results: articleList,
      total_count: count,
    } = res.data.data;
    dispatch({
      type: types.GET_ARTICLE_LIST,
      payload: {
        page,
        pageSize,
        articleList: articleList.map(item => ({
          ...item,
          cover: item.cover.images[0],
        })),
        count,
      },
    });
  };
};

export const deleteArticleAction = id => {
  return async () => {
    const res = await deleteArticle(id);
    const { message } = res.data;
    if (message === 'OK') Message.success('删除成功');
  };
};
